# delimit ;  
set more 1 ;  
local UNIX 0;
drop _all;
program drop _all;
capture log close;
* set this to 1 if include 2020 wave15;
global INCLUDE2020 0;

set mem 600m;
global folder "D:\Dropbox\";
*global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";
*global folder "C:\research";
*global folder "\\ad.ucl.ac.uk\homer\zctpmer\downloads";
*global folder "C:\Users\Jeremy\Dropbox";
*global folder "~/";

         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
		 local wave14d "$folder\hrs\wave14\";
		 local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";
		 
log using `saved'merge3.log, replace ;
*-------------------------------------------------;
*now merge in lfpr info about the intervening year;
*-------------------------------------------------;
use `wave2d'w2fc;

gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge2;
drop _merge;
*summ;
*get age in months; 
gen months1=birthyr1*12+birthm1;
gen months2=birthyr2*12+birthm2;
gen months3=birthyr3*12+birthm3;
gen months4=birthyr4*12+birthm4;
gen months5=birthyr5*12+birthm5;
gen months=months1;
replace months=months2 if months==.;
replace months=months3 if months==.;
replace months=months4 if months==.;
replace months=months5 if months==.;
sort HHID PN;
compress;
sleep 2000; save `saved'merge3, replace;
drop _all;

use `wave2d'w2fa;

gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;
drop _all;

* people who re-entered the lf between waves 1 and 2, self-employed;
use `wave2d'w2fb;

gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;


*people who left their job between waves 1 and 2;
replace W4898=. if W4898==9996|W4898>9990|W4898==0;
replace W4897=. if W4897==96|W4897==0;
replace W4897=6 if W4897==98|W4897==99;
gen quitma2=W4898*12+W4897;
*people who left self-employment;
replace W4801=. if W4801>9990|W4801==0;

****************** change made here: the following command added *********;
replace W4801=. if W4800==95; * deny being self-employed in wave 1;

replace W4800=. if W4800==96|W4800==0;
replace W4800=6 if W4800==98|W4800==99;
gen quitmb2=W4801*12+W4800;

*people who became unemployed between waves 1 and 2;
replace W3308=. if W3308==9996|W3308>9990|W3308==0;
replace W3307=. if W3307==96|W3307==0;
replace W3307=6 if W3307==98|W3307==99;
gen depa2=W3308*12+W3307;

*people who are on temporary layoff between waves 1 and 2;
replace W3311=. if W3311==9996|W3311>9990|W3311==0;
replace W3310=. if W3310==96|W3310==0;
replace W3310=6 if W3310==98|W3310==99;
gen depb2=W3311*12+W3310;

* people who are disabled between waves 1 and 2;
replace W3313=. if W3313==9996|W3313>9990|W3313==0;
replace W3312=. if W3312==96|W3312==0;
replace W3312=6 if W3312==98|W3312==99;
gen depc2=W3313*12+W3312;

* people who retired between waves 1 and 2;
replace W3315=. if W3315==9996|W3315>9990|W3315==0;
replace W3314=. if W3314==96|W3314==0;
replace W3314=6 if W3314==98|W3314==99;
gen depd2=W3315*12+W3314;

gen quitmc2=depa2;
replace quitmc2=depb2 if (depb2>quitmc2 & depb2~=.)|quitmc2==.;
replace quitmc2=depc2 if (depc2>quitmc2 & depc2~=.)|quitmc2==.;
replace quitmc2=depd2 if (depd2>quitmc2 & depd2~=.)|quitmc2==.;

gen quitm2=quitma2;
replace quitm2=quitmb2 if quitm2==. & quitmb2~=.;
replace quitm2=quitmc2 if quitm2==. & quitmc2~=.;

replace quitm2=(quitm2-months)/12;


****************************************************;
* not self-employed people who re-entered the lf between waves 1 and 2;
replace W3615=6 if W3615==98|W3615==99;
replace W3615=. if W3615==0;
replace W3616=. if W3616<1990|W3616>1999;
**** note: Li's question????????????: should it be "if W3616<1992|W3616>1999", since wave 1 time is 1992 so that the time of re-entering between wave 1 and wave 2 is later than 1992. I have the same doubt for other such re-coding too;
gen entera2=W3616*12+W3615;


* self-employed who re-entered the lf between waves 1 and 2;
replace W4327=6 if W4327==98|W4327==99;
replace W4327=. if W4327==0;
replace W4328=. if W4328<1990|W4328>1999;
gen enterb2=W4328*12+W4327;

gen enter2=entera2;
replace enter2=enterb2 if work2==2;
replace enter2=(enter2-months)/12;

*******************************************************************************;
*******************************************************************************;
*now construct labor force participation variables for the intervening years;


**************************** 1 year before wave2******************;
gen lfpra2=.;

********* in the lf or out of the lf both periods ******;
replace lfpra2=0 if hours2==0 & hours1==0;
replace lfpra2=1 if hours1>0 & hours1~=. & hours2>0 & hours2~=.;

*************** left the lf in wave2 ***********;
replace lfpra2=0 if hours2==0 & (quitm2<=realage2-1 & quitm2~=.) & realage2~=.;
replace lfpra2=1 if hours1>0 & hours1~=. & (quitm2>realage2-1) & quitm2~=. & realage2~=.;
**** Li's question???????????: Does this means that working more than 1 year between wave 1 and wave 2 is counted as 1, and less than 1 year between wave 1 and wave 2 as 0;

********* entered the labor force in wave 2 **********;
replace lfpra2=0 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (enter2~=. & enter2>=realage2-1) & realage2~=.;
replace lfpra2=1 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (enter2~=. & enter2<realage2-1) & realage2~=.;

*********************** two years before wave2 **********************;
gen lfprb2=.;

************** in the lf or out of the lf both periods ****;
replace lfprb2=0 if hours2==0 & hours1==0;
replace lfprb2=1 if hours1>0 & hours1~=. & hours2>0 & hours2~=.;

************** left the lf in wave2 ********;
replace lfprb2=0 if hours2==0 & (quitm2<=realage2-2 & quitm2~=.) & realage2~=.;
replace lfprb2=1 if hours1>0 & hours1~=. & (quitm2>realage2-2) & quitm2~=. & realage2~=.;

************** entered the labor force in wave 2 *********;
replace lfprb2=0 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (enter2~=. & enter2>=realage2-2) & realage2~=.;
replace lfprb2=1 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (enter2~=. & enter2<realage2-2) & realage2~=.;



gen ll2=.;
replace ll2=1 if lfpr1==1 & lfpr2==1 ;
replace ll2=2 if lfpr1==1 & lfpr2==0 ;
replace ll2=3 if lfpr1==0 & lfpr2==1 ;
replace ll2=4 if lfpr1==0 & lfpr2==0 ;

sort ll2;
by ll2: summ lfpra2 lfprb2 realage2 ll2 ;


drop W3* W4* W5*;
sort HHID PN;
sleep 2000; save `saved'merge3, replace;

drop _all;

*********************************************************************;
****************** WAVE 2 -> WAVE 3 *********************************;
*********************************************************************;
*people who left their job between waves 2 and 3;
use `wave3d'H96G_R;
*rename hhid HHID;
*rename pn PN;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who work for other people ********;

******** change made here ***;
* replace E2667=. if E2667==96|E2668>9990;
replace E2667=. if E2667==96|E2667==97|(E2668~=. & E2668>9990); * E2667==97 means OTHER;

replace E2667=6 if E2667==98|E2667==99;
gen quitma3=E2668*12+E2667;
replace quitma3=. if E2667==.|E2668==.;
summ quitma3;

***************** self-employed ***************************;

****** change made here ******;
* replace E2630=. if E2630==96|E2631>9990;
* replace E2630=6 if E2630==97|E2630==98|E2630==99;
replace E2630=. if E2630==96|E2630==97|(E2631~=. & E2631>9990); * E2630==97 means denying being working for self in wave 1;
replace E2630=6 if E2630==98|E2630==99;

gen quitmb3=E2631*12+E2630;
replace quitmb3=. if E2631==.|E2630==.;
summ quitmb3;

************************************************************;


*this is a set of questions about age of retirement, etc.;
replace E2612=. if E2612>12;
replace E2613=. if E2613>1996|E2613<1994;
replace E2616=. if E2616>12;
replace E2617=. if E2617>1996|E2617<1994;
replace E2619=. if E2619>12;
replace E2620=. if E2620>1996|E2620<1994;
replace E2622=. if E2622>12;
replace E2623=. if E2623>1996|E2623<1994; 

gen depa3=E2613*12+E2612;
gen depb3=E2617*12+E2616;
gen depc3=E2620*12+E2619;
gen depd3=E2623*12+E2622;

gen monthsa3=(depa3-months)/12;
gen monthsb3=(depb3-months)/12;
gen monthsc3=(depc3-months)/12;
gen monthsd3=(depd3-months)/12;

gen biggera3=monthsa3-(realage3-1);
gen biggerb3=monthsb3-(realage3-1);
gen biggerc3=monthsc3-(realage3-1);
gen biggerd3=monthsc3-(realage3-1);

sort lfpr2;
by lfpr2: summ bigger*;
sort lfpr3;
by lfpr3: summ bigger*;

gen quitmc3=depa3;
replace quitmc3=depb3 if (depb3>quitmc3 & depb3~=.)|quitmc3==.;
replace quitmc3=depc3 if (depc3>quitmc3 & depc3~=.)|quitmc3==.;
replace quitmc3=depd3 if (depd3>quitmc3 & depd3~=.)|quitmc3==.;

*now put the three quit markers together;
gen quitm3=quitma3;
replace quitm3=quitmb3 if quitm3==. & quitmb3~=.;
replace quitm3=quitmc3 if quitm3==. & quitmc3~=.;

replace quitm3=(quitm3-months)/12;


sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;


***** note: change made in the following section, and we deactivate the original section;

*******************************************************************************;
replace E2825=6 if E2825==97|E2825==98|E2825==99;
replace E2826=. if E2826<1990|E2826>1999;
gen enter3=E2826*12+E2825;
replace enter3=(enter3-months)/12;
gen agediffa=enter3-(realage3-1);

* the last variable was using "when did you start with this employer": now start with "when did you start with this kind of job";


replace E2733=6 if E2733==97|E2733==98|E2733==99;
replace E2734=. if E2734<1990|E2734>1999;
gen enter3j=E2734*12+E2733;
replace enter3j=(enter3j-months)/12;

gen agediffb=enter3j-(realage3-1);
sort lfpr2;
by lfpr2: summ agediff*;
sort lfpr3;
by lfpr3: summ agediff*;

replace enter3=enter3j if enter3==.;
replace enter3=enter3j if enter3j>enter3 & enter3~=. & enter3j~=.;
*******************************************************************************;




*******************************************************************************;
* replace E2733=6 if E2733==97|E2733==98|E2733==99;
* replace E2734=. if E2734<1990|E2734>1999;
* gen enter3=E2734*12+E2733;
* replace enter3=(enter3-months)/12;

* gen agediffa=enter3-(realage3-1);

* the last variable was using "when did you start with this employer": now start with "when did you start with this kind of job";

* replace E2825=6 if E2825==97|E2825==98|E2825==99;
* replace E2826=. if E2826<1990|E2826>1999;

* gen enter3j=E2826*12+E2825;
* replace enter3j=(enter3j-months)/12;

* gen agediffb=enter3j-(realage3-1);
* sort lfpr2;
* by lfpr2: summ agediff*;
* sort lfpr3;
* by lfpr3: summ agediff*;

* replace enter3=enter3j if enter3==.;
* replace enter3=enter3j if enter3j>enter3 & enter3~=. & enter3j~=.;
*******************************************************************************;







****************************************************************************;
****************************************************************************;
*now construct labor force participation variables for the intervening years;

****************************************** year before wave3 ********;
gen lfpra3=.;

***** those who neither entered nor exited the lf ****;
replace lfpra3=0 if hours3==0 & (hours2==0);
replace lfpra3=1 if hours3>0 & hours2>0 & hours3~=. & hours2~=.; 

***** those who exited the lf **********;
replace lfpra3=0 if hours3==0 & (quitm3<=realage3-1 & quitm3~=. & realage3~=.);
replace lfpra3=1 if hours2>0 & (quitm3>realage3-1) & quitm3~=. & realage3~=.;

******* those who entered *********;
replace lfpra3=0 if (hours2==0|hours2==.) & hours3>0 & (enter3~=. & enter3>=realage3-1) & realage3~=.;
replace lfpra3=1 if (hours2==0|hours2==.) & hours3>0 & (enter3~=. & enter3<realage3-1) & realage3~=.;
***********************************************************************;

****************************** two years before wave3 *****************;
gen lfprb3=.;
*those who neither entered nor exited the lf;
replace lfprb3=0 if hours3==0 & (hours2==0);
replace lfprb3=1 if hours3>0 & hours2>0 & hours3~=. & hours2~=.; 
*those who exited the lf;
replace lfprb3=0 if hours3==0 & (quitm3<=realage3-2 & quitm3~=. & realage3~=.);
replace lfprb3=1 if hours2>0 & (quitm3>realage3-2) & quitm3~=. & realage3~=.;
*those who entered;
replace lfprb3=0 if (hours2==0|hours2==.) & hours3>0 & (enter3~=. & enter3>=realage3-2) & realage3~=.;
replace lfprb3=1 if (hours2==0|hours2==.) & hours3>0 & (enter3~=. & enter3<realage3-2) & realage3~=.;
**************************************************************************;

gen ll3=.;
replace ll3=1 if lfpr2==1 & lfpr3==1;
replace ll3=2 if lfpr2==1 & lfpr3==0;
replace ll3=3 if lfpr2==0 & lfpr3==1;
replace ll3=4 if lfpr2==0 & lfpr3==0;

sort ll3;
by ll3: summ lfpra3 realage3 ll3;

drop E*;
sort HHID PN;
sleep 2000; save `saved'merge3, replace;
drop _all;

*********************************************************************;
****************** WAVE 3 -> WAVE 4 *********************************;
*********************************************************************;

***** change made here;
* use `wave4d'hrs98g;
use `wave4d'H98g_r;

gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;
compress;

************************* people who left their job between waves 3 and 4 ****;


************** people who work for other people ************;
replace F3188=. if F3188==96|F3189>9990;
replace F3188=6 if F3188==98|F3188==99;
gen quitma4=F3189*12+F3188;
replace quitma4=. if F3189==.|F3188==.;
summ quitma4;

********************** self-employed ***********************;

**** change made here;
* replace F3134=. if F3134==96|F3135>9990;
* replace F3134=6 if F3134==97|F3134==98|F3134==99;
replace F3134=. if F3134==96|F3134==97|F3135>9990; * F3134==97 means denying being working for self in wave 1;
replace F3134=6 if F3134==98|F3134==99;

gen quitmb4=F3135*12+F3134;
replace quitmb4=. if F3135==.|F3134==.;
summ quitmb4;

*now put the two quit markers together;
gen quitm4=quitma4;
replace quitm4=quitmb4 if quitma4==. & quitmb4~=.;

replace quitm4=(quitm4-months)/12;
*******************************************************************************;

************************************** entering the lf ************************;

********* now do people who started work in wave 4 (when did you start with your current employer?);
replace F3348=6 if F3348==98|F3348==99;
replace F3349=. if F3349<1990|F3349>1999;
gen entera4=F3349*12+F3348;
replace entera4=(entera4-months)/12;


********* now do people who started work in wave 4 (when did you start your current job?);
replace F3256=6 if F3256==98|F3256==99;
replace F3257=. if F3257<1990|F3257>1999;
gen enterb4=F3257*12+F3256;
replace enterb4=(enterb4-months)/12;

gen enter4=entera4;
replace enter4=enterb4 if entera4==. & enterb4~=.;



****************************************************************************;
****************************************************************************;
* now construct labor force participation variables for the intervening years;

********************* 1 year before wave4 *******************;
gen lfpra4=.;
gen lasthour=hours3;
replace lasthour=hours2 if (hrscoh==11|hrscoh==12|hrscoh==13);


****** either continuously working or continuously not working;
replace lfpra4=0 if hours4==0 & (lasthour==0);
replace lfpra4=1 if lasthour>0 & hours4>0 & hours4~=.& lasthour~=.;

********************* quit the lf;
replace lfpra4=0 if hours4==0 &  (quitm4<=realage4-1 & quitm4~=.) & realage4~=.;
replace lfpra4=1 if lasthour>0 & (quitm4>realage4-1 & quitm4~=.) & realage4~=.;

******************* entered the lf;
replace lfpra4=0 if (lasthour==0|lasthour==.) & hours4>0 & (enter4~=. & realage4~=. & enter4>=realage4-1);
replace lfpra4=1 if (lasthour==0|lasthour==.)  & hours4>0 & (enter4~=. & realage4~=. & enter4<realage4-1) ;
****************************************************************;

******************* two years before wave4 *********************;
gen lfprb4=.;

**** either continuously working or continuously not working;
replace lfprb4=0 if hours4==0 & (lasthour==0);
replace lfprb4=1 if lasthour>0 & hours4>0 & hours4~=. & lasthour~=.;

***** quit the lf;
replace lfprb4=0 if hours4==0 &  (quitm4<=realage4-2 & quitm4~=.) & realage4~=.;
replace lfprb4=1 if lasthour>0 & (quitm4>realage4-2 & quitm4~=.) & realage4~=.;

*******entered the lf;
replace lfprb4=0 if (lasthour==0|lasthour==.) & hours4>0 & (enter4~=. & realage4~=. & enter4>=realage4-2);
replace lfprb4=1 if (lasthour==0|lasthour==.)  & hours4>0 & (enter4~=. & realage4~=. & enter4<realage4-2) ;
*******************************************************************;

gen ll4=.; 
replace ll4=1 if lfpr3==1 & lfpr4==1;
replace ll4=2 if lfpr3==1 & lfpr4==0;
replace ll4=3 if lfpr3==0 & lfpr4==1;
replace ll4=4 if lfpr3==0 & lfpr4==0;

sort ll4;
by ll4: summ lfpra4 age4 work4 ll4;

drop F*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;
drop _all;

*********************************************************************************;
************************ WAVE 4 ---> WAVE 5 *************************************;
*******************************************************************************;
use `wave5d'h00g_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 4 and wave 5 ******;

************** people who work for other people **************; 
***************** change made here *************;
* gen quitma5=Q3438*12+Q3437;
* replace Q3437=. if Q3437==96|Q3438>9990;
* replace Q3437=6 if Q3437==98|Q3437==99;
* replace quitma5=. if Q3437==.|Q3438==.;
* summ quitma5;
replace G3437=. if G3437==96|G3437==97|G3438>9990;
replace G3437=6 if G3437==98|G3437==99;
gen quitma5=G3438*12+G3437;
replace quitma5=. if G3437==.|G3438==.;
summ quitma5;
****************************************************************;

********************* self-employed ***************************;
**** change made here;
* replace Q3384=. if Q3384==96|Q3385>9990;
* replace Q3384=6 if Q3384==97|Q3384==98|Q3384==99;
replace G3384=. if G3384==96|G3384==97|G3385>9990; * G3384==97 means denying being self-employed in wave 4;
replace G3384=6 if G3384==98|G3384==99;

gen quitmb5=G3385*12+G3384;
replace quitmb5=. if G3384==.|G3385==.;
summ quitmb5;

*now put the two quit markers together;
gen quitm5=quitma5;
replace quitm5=quitmb5 if quitma5==. & quitmb5~=.;

replace quitm5=(quitm5-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 5 (when did you start with your current employer?);

replace G3608=. if G3608<1990|G3608>1999;
replace G3607=6 if G3607==98|G3607==99;
gen entera5=G3608*12+G3607;
replace entera5=(entera5-months)/12;

* now do people who started work in wave 5(when did you start this kind of job?);

* change made here;
* replace Q3507=6 if Q3506==98|Q3506==99;
replace G3506=6 if G3506==98|G3506==99;

replace G3507=. if G3507<1990|G3507>1999;
gen enterb5=G3507*12+G3506;
replace enterb5=(enterb5-months)/12;

gen enter5=entera5;
replace enter5=enterb5 if entera5==. & enterb5~=.;

gen hchk=0;
replace hchk=1 if (hours4>0 & hours4~=.) & hours5==0;
sort hchk;
by hchk: summ enter5 entera5 enterb5 quitm5 quitma5 quitmb5;

gen echk=0;
replace echk=1 if (hours4==0 & hours5~=.) & hours5>0;
sort echk;
by echk: summ enter5 entera5 enterb5 quitm5 quitma5 quitmb5;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;

************************ 1 year before Wave5 ****************;
gen lfpra5=.;

********either continuously working or continuously not working;
replace lfpra5=0 if hours5==0 & hours4==0;
replace lfpra5=1 if hours4>0 & hours5>0 & hours5~=. & hours4~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra5=0 if hours5==0 & (quitm5<=realage5-1 & quitm5~=.) & realage5~=.;
replace lfpra5=1 if hours4>0 & (quitm5>realage5-1 & quitm5~=.) & realage5~=.;

********** entered the lf;
replace lfpra5=0 if (hours4==0|hours4==.) & hours5>0 & (enter5~=. & realage5~=. & enter5>=realage5-1);
replace lfpra5=1 if (hours4==0|hours4==.)  & hours5>0 & (enter5~=. & realage5~=. & enter5<realage5-1) ;
***************************************************************;

******************* two years before Wave5 ********************;
gen lfprb5=.;

********* either continuosuly working or continuously not working; 
replace lfprb5=0 if hours5==0 & (hours4==0);
replace lfprb5=1 if hours4>0 & hours5>0 & hours4~=. & hours4~=.;

*********** quit the lf;
replace lfprb5=0 if hours5==0 & (quitm5<=realage5-2 & quitm5~=.) & realage5~=.;
replace lfprb5=1 if hours4>0 & (quitm5>realage5-2 & quitm5~=.) & realage5~=.;

***************** entered the lf;
replace lfprb5=0 if (hours4==0|hours4==.) & hours4>0 & (enter5~=. & realage5~=. & enter5>=realage5-2);
replace lfprb5=1 if (hours4==0|hours4==.) & hours4>0 & (enter5~=. & realage5~=. & enter5<realage5-2);
****************************************************************;

gen ll5=.;
replace ll5=1 if lfpr4==1 & lfpr5==1;
replace ll5=2 if lfpr4==1 & lfpr5==0;
replace ll5=3 if lfpr4==0 & lfpr5==1;
replace ll5=4 if lfpr4==0 & lfpr5==0;

sort ll5;
by ll5: summ lfpra5 work5 ll5;

drop G3* G4*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;




*********************************************************************************;
************************ WAVE 5 ---> WAVE 6 *************************************;
*******************************************************************************;

use `wave6d'h02j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 6 ******;

************** people who work for other people **************; 
***************** change made here *************;
* gen quitma5=Q3438*12+Q3437;
* replace Q3437=. if Q3437==96|Q3438>9990;
* replace Q3437=6 if Q3437==98|Q3437==99;
* replace quitma5=. if Q3437==.|Q3438==.;
* summ quitma5;

replace HJ063=. if HJ063==92|HJ063==93|HJ063==94|HJ063==96|HJ063==97|HJ064>9990;
replace HJ063=6 if HJ063==98|HJ063==99;
gen quitma6=HJ064*12+HJ063;
replace quitma6=. if HJ063==.|HJ064==.;
summ quitma6;



****************************************************************;

********************* self-employed ***************************;
**** change made here;
* replace Q3384=. if Q3384==96|Q3385>9990;
* replace Q3384=6 if Q3384==97|Q3384==98|Q3384==99;

replace HJ023=. if HJ023==96|HJ023==97|HJ024>9990; 
replace HJ023=6 if HJ023==98|HJ023==99;

gen quitmb6=HJ024*12+HJ023;
replace quitmb6=. if HJ023==.|HJ024==.;
summ quitmb6;

*now put the two quit markers together;
gen quitm6=quitma6;
replace quitm6=quitmb6 if quitma6==. & quitmb6~=.;

replace quitm6=(quitm6-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 5 (when did you start with your current employer?);
replace HJ249=. if HJ249<1990|HJ249>1999;
replace HJ248=6 if HJ248==98|HJ248==99;
gen entera6=HJ249*12+HJ248;
replace entera6=(entera6-months)/12;

* now do people who started work in wave 5(when did you start this kind of job?);

* change made here;
* replace Q3507=6 if Q3506==98|Q3506==99;
replace HJ170=6 if HJ169==98|HJ169==99;
replace HJ170=. if HJ170<1990|HJ170>1999;
gen enterb6=HJ170*12+HJ169;
replace enterb6=(enterb6-months)/12;
gen enter6=entera6;
replace enter6=enterb6 if entera6==. & enterb6~=.;

gen hchk6=0;
replace hchk6=1 if (hours5>0 & hours5~=.) & hours6==0;
sort hchk6;
by hchk6: summ enter6 entera6 enterb6 quitm6 quitma6 quitmb6;

gen echk6=0;
replace echk6=1 if (hours5==0 & hours6~=.) & hours6>0;
sort echk6;
by echk6: summ enter6 entera6 enterb6 quitm6 quitma6 quitmb6;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave5 ****************;
gen lfpra6=.;

********either continuously working or continuously not working;
replace lfpra6=0 if hours6==0 & hours5==0;
replace lfpra6=1 if hours5>0 & hours6>0 & hours6~=. & hours5~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra6=0 if hours6==0 & (quitm6<=realage6-1 & quitm6~=.) & realage6~=.;
replace lfpra6=1 if hours5>0 & (quitm6>realage6-1 & quitm6~=.) & realage6~=.;

********** entered the lf;
replace lfpra6=0 if (hours5==0|hours5==.) & hours6>0 & (enter6~=. & realage6~=. & enter6>=realage6-1);
replace lfpra6=1 if (hours5==0|hours5==.)  & hours6>0 & (enter6~=. & realage6~=. & enter6<realage6-1) ;
***************************************************************;

******************* two years before Wave5 ********************;
gen lfprb6=.;

********* either continuosuly working or continuously not working; 
replace lfprb6=0 if hours6==0 & (hours5==0);
replace lfprb6=1 if hours5>0 & hours6>0 & hours5~=. & hours5~=.;

*********** quit the lf;
replace lfprb6=0 if hours6==0 & (quitm6<=realage6-2 & quitm6~=.) & realage6~=.;
replace lfprb6=1 if hours5>0 & (quitm6>realage6-2 & quitm6~=.) & realage6~=.;

***************** entered the lf;
replace lfprb6=0 if (hours5==0|hours5==.) & hours5>0 & (enter6~=. & realage6~=. & enter6>=realage6-2);
replace lfprb6=1 if (hours5==0|hours5==.) & hours5>0 & (enter6~=. & realage6~=. & enter6<realage6-2);
****************************************************************;

gen ll6=.;
replace ll6=1 if lfpr5==1 & lfpr6==1;
replace ll6=2 if lfpr5==1 & lfpr6==0;
replace ll6=3 if lfpr5==0 & lfpr6==1;
replace ll6=4 if lfpr5==0 & lfpr6==0;

sort ll6;
by ll6: summ lfpra6 work6 ll6;




drop HJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;






*********************************************************************************;
************************ WAVE 6 ---> WAVE 7 *************************************;
*******************************************************************************;

use `wave7d'h04j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 6 and wave 7 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace JJ063=. if JJ063==92|JJ063==93|JJ063==94|JJ063==96|JJ063==97|JJ064>9990;
replace JJ063=6 if JJ063==98|JJ063==99;
gen quitma7=JJ064*12+JJ063;
replace quitma7=. if JJ063==.|JJ064==.;
summ quitma7;



****************************************************************;

********************* self-employed ***************************;


replace JJ023=. if JJ023==96|JJ023==97|JJ024>9990; 
replace JJ023=6 if JJ023==98|JJ023==99;

gen quitmb7=JJ024*12+JJ023;
replace quitmb7=. if JJ023==.|JJ024==.;
summ quitmb7;

*now put the two quit markers together;
gen quitm7=quitma7;
replace quitm7=quitmb7 if quitma7==. & quitmb7~=.;

replace quitm7=(quitm7-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 6 (when did you start with your current employer?);
replace JJ249=. if JJ249<1990|JJ249>1999;
replace JJ248=6 if JJ248==98|JJ248==99;
gen entera7=JJ249*12+JJ248;
replace entera7=(entera7-months)/12;

* now do people who started work in wave 6(when did you start this kind of job?);

replace JJ170=6 if JJ169==98|JJ169==99;
replace JJ170=. if JJ170<1990|JJ170>1999;
gen enterb7=JJ170*12+JJ169;
replace enterb7=(enterb7-months)/12;
gen enter7=entera7;
replace enter7=enterb7 if entera7==. & enterb7~=.;

gen hchk7=0;
replace hchk7=1 if (hours6>0 & hours6~=.) & hours7==0;
sort hchk7;
by hchk7: summ enter7 entera7 enterb7 quitm7 quitma7 quitmb7;

gen echk7=0;
replace echk7=1 if (hours6==0 & hours7~=.) & hours7>0;
sort echk7;
by echk7: summ enter7 entera7 enterb7 quitm7 quitma7 quitmb7;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave6 ****************;
gen lfpra7=.;

********either continuously working or continuously not working;
replace lfpra7=0 if hours7==0 & hours6==0;
replace lfpra7=1 if hours6>0 & hours7>0 & hours7~=. & hours6~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra7=0 if hours7==0 & (quitm7<=realage7-1 & quitm7~=.) & realage7~=.;
replace lfpra7=1 if hours6>0 & (quitm7>realage7-1 & quitm7~=.) & realage7~=.;

********** entered the lf;
replace lfpra7=0 if (hours6==0|hours6==.) & hours7>0 & (enter7~=. & realage7~=. & enter7>=realage7-1);
replace lfpra7=1 if (hours6==0|hours6==.)  & hours7>0 & (enter7~=. & realage7~=. & enter7<realage7-1) ;
***************************************************************;

******************* two years before Wave6 ********************;
gen lfprb7=.;

********* either continuosuly working or continuously not working; 
replace lfprb7=0 if hours7==0 & (hours6==0);
replace lfprb7=1 if hours6>0 & hours7>0 & hours6~=. & hours6~=.;

*********** quit the lf;
replace lfprb7=0 if hours7==0 & (quitm7<=realage7-2 & quitm7~=.) & realage7~=.;
replace lfprb7=1 if hours6>0 & (quitm7>realage7-2 & quitm7~=.) & realage7~=.;

***************** entered the lf;
replace lfprb7=0 if (hours6==0|hours6==.) & hours6>0 & (enter7~=. & realage7~=. & enter7>=realage7-2);
replace lfprb7=1 if (hours6==0|hours6==.) & hours6>0 & (enter7~=. & realage7~=. & enter7<realage7-2);
****************************************************************;

gen ll7=.;
replace ll7=1 if lfpr6==1 & lfpr7==1;
replace ll7=2 if lfpr6==1 & lfpr7==0;
replace ll7=3 if lfpr6==0 & lfpr7==1;
replace ll7=4 if lfpr6==0 & lfpr7==0;

sort ll7;
by ll7: summ lfpra7 work7 ll7;




drop JJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;


*********************************************************************************;
************************ WAVE 7 ---> WAVE 8 *************************************;
*******************************************************************************;

use `wave8d'h06j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 8 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace KJ063=. if KJ063==92|KJ063==93|KJ063==94|KJ063==96|KJ063==97|KJ064>9990;
replace KJ063=6 if KJ063==98|KJ063==99;
gen quitma8=KJ064*12+KJ063;
replace quitma8=. if KJ063==.|KJ064==.;
summ quitma8;



****************************************************************;

********************* self-employed ***************************;

replace KJ023=. if KJ023==96|KJ023==97|KJ024>9990; 
replace KJ023=6 if KJ023==98|KJ023==99;

gen quitmb8=KJ024*12+KJ023;
replace quitmb8=. if KJ023==.|KJ024==.;
summ quitmb8;

*now put the two quit markers together;
gen quitm8=quitma8;
replace quitm8=quitmb8 if quitma8==. & quitmb8~=.;

replace quitm8=(quitm8-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 7 (when did you start with your current employer?);
replace KJ249=. if KJ249<1990|KJ249>1999;
replace KJ248=6 if KJ248==98|KJ248==99;
gen entera8=KJ249*12+KJ248;
replace entera8=(entera8-months)/12;

* now do people who started work in wave 7(when did you start this kind of job?);

replace KJ170=6 if KJ169==98|KJ169==99;
replace KJ170=. if KJ170<1990|KJ170>1999;
gen enterb8=KJ170*12+KJ169;
replace enterb8=(enterb8-months)/12;
gen enter8=entera8;
replace enter8=enterb8 if entera8==. & enterb8~=.;

gen hchk8=0;
replace hchk8=1 if (hours7>0 & hours7~=.) & hours8==0;
sort hchk8;
by hchk8: summ enter8 entera8 enterb8 quitm8 quitma8 quitmb8;

gen echk8=0;
replace echk8=1 if (hours7==0 & hours8~=.) & hours8>0;
sort echk8;
by echk8: summ enter8 entera8 enterb8 quitm8 quitma8 quitmb8;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave7 ****************;
gen lfpra8=.;

********either continuously working or continuously not working;
replace lfpra8=0 if hours8==0 & hours7==0;
replace lfpra8=1 if hours7>0 & hours8>0 & hours8~=. & hours7~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra8=0 if hours8==0 & (quitm8<=realage8-1 & quitm8~=.) & realage8~=.;
replace lfpra8=1 if hours7>0 & (quitm8>realage8-1 & quitm8~=.) & realage8~=.;

********** entered the lf;
replace lfpra8=0 if (hours7==0|hours7==.) & hours8>0 & (enter8~=. & realage8~=. & enter8>=realage8-1);
replace lfpra8=1 if (hours7==0|hours7==.)  & hours8>0 & (enter8~=. & realage8~=. & enter8<realage8-1) ;
***************************************************************;

******************* two years before Wave7 ********************;
gen lfprb8=.;

********* either continuosuly working or continuously not working; 
replace lfprb8=0 if hours8==0 & (hours7==0);
replace lfprb8=1 if hours7>0 & hours8>0 & hours7~=. & hours7~=.;

*********** quit the lf;
replace lfprb8=0 if hours8==0 & (quitm8<=realage8-2 & quitm8~=.) & realage8~=.;
replace lfprb8=1 if hours7>0 & (quitm8>realage8-2 & quitm8~=.) & realage8~=.;

***************** entered the lf;
replace lfprb8=0 if (hours7==0|hours7==.) & hours7>0 & (enter8~=. & realage8~=. & enter8>=realage8-2);
replace lfprb8=1 if (hours7==0|hours7==.) & hours7>0 & (enter8~=. & realage8~=. & enter8<realage8-2);
****************************************************************;

gen ll8=.;
replace ll8=1 if lfpr7==1 & lfpr8==1;
replace ll8=2 if lfpr7==1 & lfpr8==0;
replace ll8=3 if lfpr7==0 & lfpr8==1;
replace ll8=4 if lfpr7==0 & lfpr8==0;

sort ll8;
by ll8: summ lfpra8 work8 ll8;




drop KJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;

*********************************************************************************;
************************ WAVE 8 ---> WAVE 9 *************************************;
*******************************************************************************;

use `wave9d'h08j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 9 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace LJ063=. if LJ063==92|LJ063==93|LJ063==94|LJ063==96|LJ063==97|LJ064>9990;
replace LJ063=6 if LJ063==98|LJ063==99;
gen quitma9=LJ064*12+LJ063;
replace quitma9=. if LJ063==.|LJ064==.;
summ quitma9;



****************************************************************;

********************* self-employed ***************************;

replace LJ023=. if LJ023==96|LJ023==97|LJ024>9990; 
replace LJ023=6 if LJ023==98|LJ023==99;

gen quitmb9=LJ024*12+LJ023;
replace quitmb9=. if LJ023==.|LJ024==.;
summ quitmb9;

*now put the two quit markers together;
gen quitm9=quitma9;
replace quitm9=quitmb9 if quitma9==. & quitmb9~=.;

replace quitm9=(quitm9-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 8 (when did you start with your current employer?);
replace LJ249=. if LJ249<1990|LJ249>1999;
replace LJ248=6 if LJ248==98|LJ248==99;
gen entera9=LJ249*12+LJ248;
replace entera9=(entera9-months)/12;

* now do people who started work in wave 8(when did you start this kind of job?);

replace LJ170=6 if LJ169==98|LJ169==99;
replace LJ170=. if LJ170<1990|LJ170>1999;
gen enterb9=LJ170*12+LJ169;
replace enterb9=(enterb9-months)/12;
gen enter9=entera9;
replace enter9=enterb9 if entera9==. & enterb9~=.;

gen hchk9=0;
replace hchk9=1 if (hours8>0 & hours8~=.) & hours9==0;
sort hchk9;
by hchk9: summ enter9 entera9 enterb9 quitm9 quitma9 quitmb9;

gen echk9=0;
replace echk9=1 if (hours8==0 & hours9~=.) & hours9>0;
sort echk9;
by echk9: summ enter9 entera9 enterb9 quitm9 quitma9 quitmb9;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave8 ****************;
gen lfpra9=.;

********either continuously working or continuously not working;
replace lfpra9=0 if hours9==0 & hours8==0;
replace lfpra9=1 if hours8>0 & hours9>0 & hours9~=. & hours8~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra9=0 if hours9==0 & (quitm9<=realage9-1 & quitm9~=.) & realage9~=.;
replace lfpra9=1 if hours8>0 & (quitm9>realage9-1 & quitm9~=.) & realage9~=.;

********** entered the lf;
replace lfpra9=0 if (hours8==0|hours8==.) & hours9>0 & (enter9~=. & realage9~=. & enter9>=realage9-1);
replace lfpra9=1 if (hours8==0|hours8==.)  & hours9>0 & (enter9~=. & realage9~=. & enter9<realage9-1) ;
***************************************************************;

******************* two years before Wave8 ********************;
gen lfprb9=.;

********* either continuosuly working or continuously not working; 
replace lfprb9=0 if hours9==0 & (hours8==0);
replace lfprb9=1 if hours8>0 & hours9>0 & hours8~=. & hours8~=.;

*********** quit the lf;
replace lfprb9=0 if hours9==0 & (quitm9<=realage9-2 & quitm9~=.) & realage9~=.;
replace lfprb9=1 if hours8>0 & (quitm9>realage9-2 & quitm9~=.) & realage9~=.;

***************** entered the lf;
replace lfprb9=0 if (hours8==0|hours8==.) & hours8>0 & (enter9~=. & realage9~=. & enter9>=realage9-2);
replace lfprb9=1 if (hours8==0|hours8==.) & hours8>0 & (enter9~=. & realage9~=. & enter9<realage9-2);
****************************************************************;

gen ll9=.;
replace ll9=1 if lfpr8==1 & lfpr9==1;
replace ll9=2 if lfpr8==1 & lfpr9==0;
replace ll9=3 if lfpr8==0 & lfpr9==1;
replace ll9=4 if lfpr8==0 & lfpr9==0;

sort ll9;
by ll9: summ lfpra9 work9 ll9;




drop LJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;



*********************************************************************************;
************************ WAVE 9 ---> WAVE 10 *************************************;
*******************************************************************************;

use `wave10d'h10j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 10 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace MJ063=. if MJ063==92|MJ063==93|MJ063==94|MJ063==96|MJ063==97|MJ064>9990;
replace MJ063=6 if MJ063==98|MJ063==99;
gen quitma10=MJ064*12+MJ063;
replace quitma10=. if MJ063==.|MJ064==.;
summ quitma10;



****************************************************************;

********************* self-employed ***************************;

replace MJ023=. if MJ023==96|MJ023==97|MJ024>9990; 
replace MJ023=6 if MJ023==98|MJ023==99;

gen quitmb10=MJ024*12+MJ023;
replace quitmb10=. if MJ023==.|MJ024==.;
summ quitmb10;

*now put the two quit markers together;
gen quitm10=quitma10;
replace quitm10=quitmb10 if quitma10==. & quitmb10~=.;

replace quitm10=(quitm10-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 9 (when did you start with your current employer?);
replace MJ249=. if MJ249<1990|MJ249>1999;
replace MJ248=6 if MJ248==98|MJ248==99;
gen entera10=MJ249*12+MJ248;
replace entera10=(entera10-months)/12;

* now do people who started work in wave 9(when did you start this kind of job?);

replace MJ170=6 if MJ169==98|MJ169==99;
replace MJ170=. if MJ170<1990|MJ170>1999;
gen enterb10=MJ170*12+MJ169;
replace enterb10=(enterb10-months)/12;
gen enter10=entera10;
replace enter10=enterb10 if entera10==. & enterb10~=.;

gen hchk10=0;
replace hchk10=1 if (hours9>0 & hours9~=.) & hours10==0;
sort hchk10;
by hchk10: summ enter10 entera10 enterb10 quitm10 quitma10 quitmb10;

gen echk10=0;
replace echk10=1 if (hours9==0 & hours10~=.) & hours10>0;
sort echk10;
by echk10: summ enter10 entera10 enterb10 quitm10 quitma10 quitmb10;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave8 ****************;
gen lfpra10=.;

********either continuously working or continuously not working;
replace lfpra10=0 if hours10==0 & hours9==0;
replace lfpra10=1 if hours9>0 & hours10>0 & hours10~=. & hours9~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra10=0 if hours10==0 & (quitm10<=realage10-1 & quitm10~=.) & realage10~=.;
replace lfpra10=1 if hours9>0 & (quitm10>realage10-1 & quitm10~=.) & realage10~=.;

********** entered the lf;
replace lfpra10=0 if (hours9==0|hours9==.) & hours10>0 & (enter10~=. & realage10~=. & enter10>=realage10-1);
replace lfpra10=1 if (hours9==0|hours9==.)  & hours10>0 & (enter10~=. & realage10~=. & enter10<realage10-1) ;
***************************************************************;

******************* two years before Wave8 ********************;
gen lfprb10=.;

********* either continuosuly working or continuously not working; 
replace lfprb10=0 if hours10==0 & (hours9==0);
replace lfprb10=1 if hours9>0 & hours10>0 & hours9~=. & hours9~=.;

*********** quit the lf;
replace lfprb10=0 if hours10==0 & (quitm10<=realage10-2 & quitm10~=.) & realage10~=.;
replace lfprb10=1 if hours9>0 & (quitm10>realage10-2 & quitm10~=.) & realage10~=.;

***************** entered the lf;
replace lfprb10=0 if (hours9==0|hours9==.) & hours9>0 & (enter10~=. & realage10~=. & enter10>=realage10-2);
replace lfprb10=1 if (hours9==0|hours9==.) & hours9>0 & (enter10~=. & realage10~=. & enter10<realage10-2);
****************************************************************;

gen ll10=.;
replace ll10=1 if lfpr9==1 & lfpr10==1;
replace ll10=2 if lfpr9==1 & lfpr10==0;
replace ll10=3 if lfpr9==0 & lfpr10==1;
replace ll10=4 if lfpr9==0 & lfpr10==0;

sort ll10;
by ll10: summ lfpra10 work10 ll10;




drop MJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;




*********************************************************************************;
************************ WAVE 10 ---> WAVE 11 *************************************;
*******************************************************************************;

use `wave11d'h12j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 11 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace NJ063=. if NJ063==92|NJ063==93|NJ063==94|NJ063==96|NJ063==97|NJ064>9990;
replace NJ063=6 if NJ063==98|NJ063==99;
gen quitma11=NJ064*12+NJ063;
replace quitma11=. if NJ063==.|NJ064==.;
summ quitma11;



****************************************************************;

********************* self-employed ***************************;

replace NJ023=. if NJ023==96|NJ023==97|NJ024>9990; 
replace NJ023=6 if NJ023==98|NJ023==99;

gen quitmb11=NJ024*12+NJ023;
replace quitmb11=. if NJ023==.|NJ024==.;
summ quitmb11;

*now put the two quit markers together;
gen quitm11=quitma11;
replace quitm11=quitmb11 if quitma11==. & quitmb11~=.;

replace quitm11=(quitm11-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 10 (when did you start with your current employer?);
replace NJ249=. if NJ249<1990|NJ249>1999;
replace NJ248=6 if NJ248==98|NJ248==99;
gen entera11=NJ249*12+NJ248;
replace entera11=(entera11-months)/12;

* now do people who started work in wave 10(when did you start this kind of job?);

replace NJ170=6 if NJ169==98|NJ169==99;
replace NJ170=. if NJ170<1990|NJ170>1999;
gen enterb11=NJ170*12+NJ169;
replace enterb11=(enterb11-months)/12;
gen enter11=entera11;
replace enter11=enterb11 if entera11==. & enterb11~=.;

gen hchk11=0;
replace hchk11=1 if (hours10>0 & hours10~=.) & hours11==0;
sort hchk11;
by hchk11: summ enter11 entera11 enterb11 quitm11 quitma11 quitmb11;

gen echk11=0;
replace echk11=1 if (hours10==0 & hours11~=.) & hours11>0;
sort echk11;
by echk11: summ enter11 entera11 enterb11 quitm11 quitma11 quitmb11;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave8 ****************;
gen lfpra11=.;

********either continuously working or continuously not working;
replace lfpra11=0 if hours11==0 & hours10==0;
replace lfpra11=1 if hours10>0 & hours11>0 & hours11~=. & hours10~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra11=0 if hours11==0 & (quitm11<=realage11-1 & quitm11~=.) & realage11~=.;
replace lfpra11=1 if hours10>0 & (quitm11>realage11-1 & quitm11~=.) & realage11~=.;

********** entered the lf;
replace lfpra11=0 if (hours10==0|hours10==.) & hours11>0 & (enter11~=. & realage11~=. & enter11>=realage11-1);
replace lfpra11=1 if (hours10==0|hours10==.)  & hours11>0 & (enter11~=. & realage11~=. & enter11<realage11-1) ;
***************************************************************;

******************* two years before Wave10 ********************;
gen lfprb11=.;

********* either continuosuly working or continuously not working; 
replace lfprb11=0 if hours11==0 & (hours10==0);
replace lfprb11=1 if hours10>0 & hours11>0 & hours10~=. & hours10~=.;

*********** quit the lf;
replace lfprb11=0 if hours11==0 & (quitm11<=realage11-2 & quitm11~=.) & realage11~=.;
replace lfprb11=1 if hours10>0 & (quitm11>realage11-2 & quitm11~=.) & realage11~=.;

***************** entered the lf;
replace lfprb11=0 if (hours10==0|hours10==.) & hours10>0 & (enter11~=. & realage11~=. & enter11>=realage11-2);
replace lfprb11=1 if (hours10==0|hours10==.) & hours10>0 & (enter11~=. & realage11~=. & enter11<realage11-2);
****************************************************************;

gen ll11=.;
replace ll11=1 if lfpr10==1 & lfpr11==1;
replace ll11=2 if lfpr10==1 & lfpr11==0;
replace ll11=3 if lfpr10==0 & lfpr11==1;
replace ll11=4 if lfpr10==0 & lfpr11==0;

sort ll11;
by ll11: summ lfpra11 work11 ll11;




drop NJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;

*********************************************************************************;
************************ WAVE 11 ---> WAVE 12 *************************************;
*******************************************************************************;

use `wave12d'h14j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 12 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace OJ063=. if OJ063==92|OJ063==93|OJ063==94|OJ063==96|OJ063==97|OJ064>9990;
replace OJ063=6 if OJ063==98|OJ063==99;
gen quitma12=OJ064*12+OJ063;
replace quitma12=. if OJ063==.|OJ064==.;
summ quitma12;



****************************************************************;

********************* self-employed ***************************;

replace OJ023=. if OJ023==96|OJ023==97|OJ024>9990; 
replace OJ023=6 if OJ023==98|OJ023==99;

gen quitmb12=OJ024*12+OJ023;
replace quitmb12=. if OJ023==.|OJ024==.;
summ quitmb12;

*now put the two quit markers together;
gen quitm12=quitma12;
replace quitm12=quitmb12 if quitma12==. & quitmb12~=.;

replace quitm12=(quitm12-months)/12;
*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 11 (when did you start with your current employer?);
replace OJ249=. if OJ249<1990|OJ249>1999;
replace OJ248=6 if OJ248==98|OJ248==99;
gen entera12=OJ249*12+OJ248;
replace entera12=(entera12-months)/12;

* now do people who started work in wave 10(when did you start this kind of job?);

replace OJ170=6 if OJ169==98|OJ169==99;
replace OJ170=. if OJ170<1990|OJ170>1999;
gen enterb12=OJ170*12+OJ169;
replace enterb12=(enterb12-months)/12;
gen enter12=entera12;
replace enter12=enterb12 if entera12==. & enterb12~=.;

gen hchk12=0;
replace hchk12=1 if (hours11>0 & hours11~=.) & hours12==0;
sort hchk12;
by hchk12: summ enter12 entera12 enterb12 quitm12 quitma12 quitmb12;

gen echk12=0;
replace echk12=1 if (hours11==0 & hours12~=.) & hours12>0;
sort echk12;
by echk12: summ enter12 entera12 enterb12 quitm12 quitma12 quitmb12;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave8 ****************;
gen lfpra12=.;

********either continuously working or continuously not working;
replace lfpra12=0 if hours12==0 & hours11==0;
replace lfpra12=1 if hours11>0 & hours12>0 & hours12~=. & hours11~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra12=0 if hours12==0 & (quitm12<=realage12-1 & quitm12~=.) & realage12~=.;
replace lfpra12=1 if hours11>0 & (quitm12>realage12-1 & quitm12~=.) & realage12~=.;

********** entered the lf;
replace lfpra12=0 if (hours11==0|hours11==.) & hours12>0 & (enter12~=. & realage12~=. & enter12>=realage12-1);
replace lfpra12=1 if (hours11==0|hours11==.)  & hours12>0 & (enter12~=. & realage12~=. & enter12<realage12-1) ;
***************************************************************;

******************* two years before Wave11 ********************;
gen lfprb12=.;

********* either continuosuly working or continuously not working; 
replace lfprb12=0 if hours12==0 & (hours11==0);
replace lfprb12=1 if hours11>0 & hours12>0 & hours11~=. & hours11~=.;

*********** quit the lf;
replace lfprb12=0 if hours12==0 & (quitm12<=realage12-2 & quitm12~=.) & realage12~=.;
replace lfprb12=1 if hours11>0 & (quitm12>realage12-2 & quitm12~=.) & realage12~=.;

***************** entered the lf;
replace lfprb12=0 if (hours11==0|hours11==.) & hours11>0 & (enter12~=. & realage12~=. & enter12>=realage12-2);
replace lfprb12=1 if (hours11==0|hours11==.) & hours11>0 & (enter12~=. & realage12~=. & enter12<realage12-2);
****************************************************************;

gen ll12=.;
replace ll12=1 if lfpr11==1 & lfpr12==1;
replace ll12=2 if lfpr11==1 & lfpr12==0;
replace ll12=3 if lfpr11==0 & lfpr12==1;
replace ll12=4 if lfpr11==0 & lfpr12==0;

sort ll12;
by ll12: summ lfpra12 work12 ll12;




drop OJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;



*********************************************************************************;
************************ WAVE 12 ---> WAVE 13 *************************************;
*******************************************************************************;

use `wave13d'h16j_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 13 ******;

************** people who work for other people **************; 
***************** change made here *************;


replace PJ063=. if PJ063==92|PJ063==93|PJ063==94|PJ063==96|PJ063==97|PJ064>9990;
replace PJ063=6 if PJ063==98|PJ063==99;
gen quitma13=PJ064*12+PJ063;
replace quitma13=. if PJ063==.|PJ064==.;
summ quitma13;



****************************************************************;

********************* self-employed ***************************;

replace PJ023=. if PJ023==96|PJ023==97|PJ024>9990; 
replace PJ023=6 if PJ023==98|PJ023==99;

gen quitmb13=PJ024*12+PJ023;
replace quitmb13=. if PJ023==.|PJ024==.;
summ quitmb13;

*now put the two quit markers together;
gen quitm13=quitma13;
replace quitm13=quitmb13 if quitma13==. & quitmb13~=.;

replace quitm13=(quitm13-months)/12;

*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in wave 11 (when did you start with your current employer?);
replace PJ249=. if PJ249<1990|PJ249>1999;
replace PJ248=6 if PJ248==98|PJ248==99;
gen entera13=PJ249*12+PJ248;
replace entera13=(entera13-months)/12;

* now do people who started work in wave 10(when did you start this kind of job?);

replace PJ170=6 if PJ169==98|PJ169==99;
replace PJ170=. if PJ170<1990|PJ170>1999;
gen enterb13=PJ170*12+PJ169;
replace enterb13=(enterb13-months)/12;
gen enter13=entera13;
replace enter13=enterb13 if entera13==. & enterb13~=.;

gen hchk13=0;
replace hchk13=1 if (hours12>0 & hours12~=.) & hours13==0;
sort hchk13;
by hchk13: summ enter13 entera13 enterb13 quitm13 quitma13 quitmb13;

gen echk13=0;
replace echk13=1 if (hours12==0 & hours13~=.) & hours13>0;
sort echk13;
by echk13: summ enter13 entera13 enterb13 quitm13 quitma13 quitmb13;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave8 ****************;
gen lfpra13=.;

********either continuously working or continuously not working;
replace lfpra13=0 if hours13==0 & hours12==0;
replace lfpra13=1 if hours12>0 & hours13>0 & hours13~=. & hours12~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra13=0 if hours13==0 & (quitm13<=realage13-1 & quitm13~=.) & realage13~=.;
replace lfpra13=1 if hours12>0 & (quitm13>realage13-1 & quitm13~=.) & realage13~=.;

********** entered the lf;
replace lfpra13=0 if (hours12==0|hours12==.) & hours13>0 & (enter13~=. & realage13~=. & enter13>=realage13-1);
replace lfpra13=1 if (hours12==0|hours12==.)  & hours13>0 & (enter13~=. & realage13~=. & enter13<realage13-1) ;
***************************************************************;

******************* two years before Wave11 ********************;
gen lfprb13=.;

********* either continuosuly working or continuously not working; 
replace lfprb13=0 if hours13==0 & (hours12==0);
replace lfprb13=1 if hours12>0 & hours13>0 & hours12~=. & hours12~=.;

*********** quit the lf;
replace lfprb13=0 if hours13==0 & (quitm13<=realage13-2 & quitm13~=.) & realage13~=.;
replace lfprb13=1 if hours12>0 & (quitm13>realage13-2 & quitm13~=.) & realage13~=.;

***************** entered the lf;
replace lfprb13=0 if (hours12==0|hours12==.) & hours12>0 & (enter13~=. & realage13~=. & enter13>=realage13-2);
replace lfprb13=1 if (hours12==0|hours12==.) & hours12>0 & (enter13~=. & realage13~=. & enter13<realage13-2);
****************************************************************;

gen ll13=.;
replace ll13=1 if lfpr12==1 & lfpr13==1;
replace ll13=2 if lfpr12==1 & lfpr13==0;
replace ll13=3 if lfpr12==0 & lfpr13==1;
replace ll13=4 if lfpr12==0 & lfpr13==0;

sort ll13;
by ll13: summ lfpra13 work13 ll13;




drop PJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;




*********************************************************************************;
************************ WAVE 13 ---> WAVE 14 *************************************;
*******************************************************************************;

use `wave14d'h18j_r;
gen temp=real(hhid);
drop hhid;
gen HHID=temp;
drop temp;
gen temp=real(pn);
drop pn;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;

******************* people who left their jobs between wave 5 and wave 14 ******;

************** people who work for other people **************; 
***************** change made here *************;
*QJ064;
*           52                      -8.  Web non-response;
*         2224               2007-2019.  Actual Value
*          116                    9998.  DK (Don't Know) NA (Not Ascertained);
*           15                    9999.  RF (Refused);
*        14739                   Blank.  INAP (Inapplicable)  Partial Interview;
* Chris added the |QJ064<0 because of above;
* and did similar for QJ024;
replace QJ063=. if QJ063==92|QJ063==93|QJ063==94|QJ063==96|QJ063==97|QJ064>9990|QJ064<0;
replace QJ063=6 if QJ063==98|QJ063==99;
gen quitma14=QJ064*12+QJ063;
replace quitma14=. if QJ063==.|QJ064==.;
summ quitma14;

****************************************************************;
********************* self-employed ***************************;

replace QJ023=. if QJ023==96|QJ023==97|QJ024>9990|QJ024<0; 
replace QJ023=6 if QJ023==98|QJ023==99;

gen quitmb14=QJ024*12+QJ023;
replace quitmb14=. if QJ023==.|QJ024==.;
summ quitmb14;

*now put the two quit markers together;
gen quitm14=quitma14;
replace quitm14=quitmb14 if quitma14==. & quitmb14~=.;

replace quitm14=(quitm14-months)/12;

*******************************************************************************;

*******************************************************************************;
*******************************************************************************;
*now do people who started work in this wave (when did you start with your current employer?);
*QJ249;                    
*           18                      -8.  Web non-response;
*         2445               1917-2019.  Actual Value;
*           47                    9998.  DK (Don't Know)  NA (Not Ascertained);
*            6                    9999.  RF (Refused);
*        14630                   Blank.  INAP (Inapplicable)  Partial Interview;

replace QJ249=. if QJ249<1990|QJ249>1999;
replace QJ248=6 if QJ248==98|QJ248==99;
gen entera14=QJ249*12+QJ248;
replace entera14=(entera14-months)/12;

* now do people who started work in this wave(when did you start this kind of job?);

replace QJ170=6 if QJ169==98|QJ169==99;
replace QJ170=. if QJ170<1990|QJ170>1999;
gen enterb14=QJ170*12+QJ169;
replace enterb14=(enterb14-months)/12;
gen enter14=entera14;
replace enter14=enterb14 if entera14==. & enterb14~=.;

gen hchk14=0;
replace hchk14=1 if (hours12>0 & hours12~=.) & hours14==0;
sort hchk14;
by hchk14: summ enter14 entera14 enterb14 quitm14 quitma14 quitmb14;

gen echk14=0;
replace echk14=1 if (hours12==0 & hours14~=.) & hours14>0;
sort echk14;
by echk14: summ enter14 entera14 enterb14 quitm14 quitma14 quitmb14;


sort HHID PN;
sleep 2000; save `saved'merge3, replace; 
*******************************************************************************;*******************************************************************************;
*now construct labor force participation variables for the intervening years;
************************ 1 year before Wave8 ****************;
gen lfpra14=.;

********either continuously working or continuously not working;
replace lfpra14=0 if hours14==0 & hours12==0;
replace lfpra14=1 if hours12>0 & hours14>0 & hours14~=. & hours12~=.;

***** This is also defined in demog.do & explanation is provided there as well;
replace lfpra14=0 if hours14==0 & (quitm14<=realage14-1 & quitm14~=.) & realage14~=.;
replace lfpra14=1 if hours12>0 & (quitm14>realage14-1 & quitm14~=.) & realage14~=.;

********** entered the lf;
replace lfpra14=0 if (hours12==0|hours12==.) & hours14>0 & (enter14~=. & realage14~=. & enter14>=realage14-1);
replace lfpra14=1 if (hours12==0|hours12==.)  & hours14>0 & (enter14~=. & realage14~=. & enter14<realage14-1) ;
***************************************************************;

******************* two years before Wave11 ********************;
gen lfprb14=.;

********* either continuosuly working or continuously not working; 
replace lfprb14=0 if hours14==0 & (hours12==0);
replace lfprb14=1 if hours12>0 & hours14>0 & hours12~=. & hours12~=.;

*********** quit the lf;
replace lfprb14=0 if hours14==0 & (quitm14<=realage14-2 & quitm14~=.) & realage14~=.;
replace lfprb14=1 if hours12>0 & (quitm14>realage14-2 & quitm14~=.) & realage14~=.;

***************** entered the lf;
replace lfprb14=0 if (hours12==0|hours12==.) & hours12>0 & (enter14~=. & realage14~=. & enter14>=realage14-2);
replace lfprb14=1 if (hours12==0|hours12==.) & hours12>0 & (enter14~=. & realage14~=. & enter14<realage14-2);
****************************************************************;

gen ll14=.;
replace ll14=1 if lfpr12==1 & lfpr14==1;
replace ll14=2 if lfpr12==1 & lfpr14==0;
replace ll14=3 if lfpr12==0 & lfpr14==1;
replace ll14=4 if lfpr12==0 & lfpr14==0;

sort ll14;
by ll14: summ lfpra14 work14 ll14;




drop QJ*;

sort HHID PN;
sleep 2000; save `saved'merge3, replace;
******************** END WAVE 14 ;









**************************************** ahead wave 2 *************************;
*******************************************************************************;



use `aheadw2'A95g_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
merge HHID PN using `saved'merge3;
drop _merge;



* people who re-entered the lf between waves 1 and 2, self-employed;


* people who left their job between waves 1 and 2;
replace D2760=. if D2760>9996;
replace D2759=. if D2759==96;
replace D2759=6 if D2759==97|D2759==98|D2759==99;
gen aquitma2=D2760*12+D2759;

*people who left self-employment;
replace D2655=. if D2655>9996;
replace D2654=. if D2654==96;
replace D2654=6 if D2654==97|D2654==98|D2654==99;
gen aquitmb2=D2655*12+D2654;

*people who became unemployed between waves 1 and 2;
replace D2628=. if D2628>9996;
replace D2627=6 if D2627==97|D2627==98|D2627==99;
gen adepa2=D2628*12+D2627;

* people who are on temporary layoff between waves 1 and 2;
replace D2634=. if D2634>9996;
replace D2633=6 if D2633==97|D2633==98|D2633==99;
gen adepb2=D2634*12+D2633;

* people who are disabled between waves 1 and 2;
replace D2639=. if D2639>9996;
replace D2638=6 if D2638==97|D2638==98|D2638==99;
gen adepc2=D2639*12+D2638;

* people who retired between waves 1 and 2;
replace D2643=. if D2643>9994;
replace D2644=6 if D2644==97|D2644==98|D2644==99;
gen adepd2=D2643*12+D2644; * note: unlike elsewhere, here D2644 is month varaible;

gen aquitmc2=adepa2;
replace aquitmc2=adepb2 if (adepb2>aquitmc2 & adepb2~=.)|aquitmc2==.;
replace aquitmc2=adepc2 if (adepc2>aquitmc2 & adepc2~=.)|aquitmc2==.;
replace aquitmc2=adepd2 if (adepd2>quitmc2 & depd2~=.)|aquitmc2==.;

gen aquitm2=aquitma2;
replace aquitm2=aquitmb2 if aquitm2==. & aquitmb2~=.;
replace aquitm2=aquitmc2 if aquitm2==. & aquitmc2~=.;

replace aquitm2=(aquitm2-months)/12;





************ re-entering into the labor force ********************;

* not self-employed people who re-entered the lf between waves 1 and 2 (when did you start working for this employer?);
replace D2914=6 if D2914==97|D2914==98|D2914==99;
replace D2915=. if D2915<1990|D2915>1999;
**** note: Li's question????????????: should it be "if D2915<1993|D2915>1999", since ahead wave 1 time is 1993 so that the time of re-entering between wave 1 and wave 2 is later than 1993. I have the same doubt for other such re-coding too;
gen aentera2=D2915*12+D2914;


* not self-employed people who re-entered the lf between waves 1 and 2 (when did you start doing this kind of job?);
replace D2831=6 if D2831==97|D2831==98|D2831==99;
replace D2832=. if D2832<1990|D2832>1999;
**** note: Li's question????????????: should it be "if D2832<1993|D2832>1999", since ahead wave 1 time is 1993 so that the time of re-entering between wave 1 and wave 2 is later than 1993. I have the same doubt for other such re-coding too;
gen aenterb2=D2832*12+D2831;



* self-employed who re-entered the lf between waves 1 and 2;
replace D3273=6 if D3273==97|D3273==98|D3273==99;
replace D3274=. if D3274<1990|D3274>1999;
gen aenterc2=D3274*12+D3273;

gen aenter2=entera2;
replace aenter2=aenterb2 if aenter2==. & aenterb2~=.;
replace aenter2=aenterc2 if aenter2==. & aenterc2~=.;
replace aenter2=(aenter2-months)/12;

*******************************************************************************;
*******************************************************************************;
*now construct labor force participation variables for the intervening years;


**************************** 1 year before wave2******************;
gen alfpra2=.;

********* in the lf or out of the lf both periods ******;
replace alfpra2=0 if hours2==0 & hours1==0;
replace alfpra2=1 if hours1>0 & hours1~=. & hours2>0 & hours2~=.;

*************** left the lf in wave2 ***********;
replace alfpra2=0 if hours2==0 & (aquitm2<=realage2-1 & aquitm2~=.) & realage2~=.;
replace alfpra2=1 if hours1>0 & hours1~=. & (aquitm2>realage2-1) & aquitm2~=. & realage2~=.;
**** Li's question???????????: Does this means that working more than 1 year between wave 1 and wave 2 is counted as 1, and less than 1 year between wave 1 and wave 2 as 0;

********* entered the labor force in wave 2 **********;
replace alfpra2=0 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (aenter2~=. & aenter2>=realage2-1) & realage2~=.;
replace alfpra2=1 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (aenter2~=. & aenter2<realage2-1) & realage2~=.;

*********************** two years before wave2 **********************;
gen alfprb2=.;

************** in the lf or out of the lf both periods ****;
replace alfprb2=0 if hours2==0 & hours1==0;
replace alfprb2=1 if hours1>0 & hours1~=. & hours2>0 & hours2~=.;

************** left the lf in wave2 ********;
replace alfprb2=0 if hours2==0 & (aquitm2<=realage2-2 & aquitm2~=.) & realage2~=.;
replace alfprb2=1 if hours1>0 & hours1~=. & (aquitm2>realage2-2) & aquitm2~=. & realage2~=.;

************** entered the labor force in wave 2 *********;
replace alfprb2=0 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (aenter2~=. & aenter2>=realage2-2) & realage2~=.;
replace alfprb2=1 if (hours1==0|hours1==.) & (hours2>0 & hours2~=.) & (aenter2~=. & aenter2<realage2-2) & realage2~=.;


gen all2=.;
replace all2=1 if lfpr1==1 & lfpr2==1 ;
replace all2=2 if lfpr1==1 & lfpr2==0 ;
replace all2=3 if lfpr1==0 & lfpr2==1 ;
replace all2=4 if lfpr1==0 & lfpr2==0 ;

sort all2;
by all2: summ alfpra2 alfprb2 realage2 all2 ;


*** replace hrs wave 2 data with ahead wave 2 data;
replace ll2=all2 if ll2==. & all2~=.;
replace lfpra2=alfpra2 if lfpra2==. & alfpra2~=.;
replace lfprb2=alfprb2 if lfprb2==. & alfprb2~=.;


drop D*;
sort HHID PN;
sleep 2000; save `saved'merge3, replace;
log close;




